package com.example.My.tools;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Random;

/**
 * Author: 谢宇
 * CreatTime 2023/11/18 21:56
 * Version 1.0.0
 */
public class RandomTool {

    private static final String stringCode;

    private static final String stringAndNumCode;

    private static final HashMap<Integer,String> map = new HashMap<>();

    public Integer codeLength;


    public Integer type;

    static  {
        stringCode="abcdefghijklmnopqrstuvwxyz";

        stringAndNumCode=stringCode+"123456789";

        map.put(1,stringCode);
        map.put(2,stringAndNumCode);
    }

    public RandomTool(Integer codeLength) {
        this.codeLength = codeLength;
    }

    public RandomTool(Integer codeLength, Integer type) {
        this.codeLength = codeLength;
        this.type = type;
    }

    public String randomNum(){
        String reString="";
        Random random=new Random();
        //生成6位随机验证码
        for (int i = 0; i < codeLength; i++) {
            int item = random.nextInt(9);
            reString +=item;
        }
        return reString;
    }

    /**
     * todo 传入参数type为1 返回字符串的验证码，参数type为2 返回数字加字符串的验证码
     */
    public String randomString(){
        String reString="";
        Random random =new Random();
        if(this.type==null){
            this.type=1;
        }
        String[] split = map.get(this.type).split("");
        for (int i = 0; i < codeLength; i++) {
            String s = split[random.nextInt(split.length)];
            reString +=s;
        }
        return reString;
    }
}
